{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Min Range Needed to Sort（最小排序范围）][1]\n",
    "\n",
    "[@mjd507](https://github.com/mjd507)\n",
    "\n",
    "![Logo](DP-67.jpg)\n",
    "\n",
    "Given a list of integers, return the bounds of the minimum range that must be sorted so that the whole list would be sorted.\n",
    "\n",
    "&emsp;&emsp;给定一个整数列表，返回需要重排序的最小区间。\n",
    "\n",
    "[1]: https://mp.weixin.qq.com/s/QKS11_SCmSTHSxgtV88ljg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```text\n",
    "Input: [1, 7, 9, 5, 7, 8, 10]\n",
    "Output: (1, 5)\n",
    "```\n",
    "\n",
    "The numbers between index `1` and `5` are out of order and need to be sorted.\n",
    "\n",
    "&emsp;&emsp;索引`1`到`5`之间为乱序，需要重排。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution from starting point（基于模板的解决方案）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def findRange(nums: list) -> tuple:\n",
    "    result = 0\n",
    "    insertion = len(nums)\n",
    "\n",
    "    for temp in range(len(nums)):\n",
    "        if temp == 0 or nums[temp] > max(nums[:temp]):\n",
    "            pass\n",
    "        else:\n",
    "            result = max(result, temp)\n",
    "            for index in range(temp):\n",
    "                if nums[temp] < nums[index]:\n",
    "                    insertion = min(insertion, index)\n",
    "                    break\n",
    "    return (insertion, result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 5)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "findRange([1, 7, 9, 5, 7, 8, 10])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
